package com.xiaolei.library.Base;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.asynclayoutinflater.view.AsyncLayoutInflater;

import com.xiaolei.library.Exts.Ext;
import com.xiaolei.library.LifeCycle.ObserverNoneNull;
import com.xiaolei.library.ViewModels.BaseCycleViewModel;
import com.xiaolei.library.ViewModels.BaseCycleViewModel.Status;

/**
 * 使用异步的方式，对界面 xml 进行初始化,加速界面进入避免卡顿掉帧
 */
public abstract class AsyncBaseActivity extends AppCompatActivity implements BaseCycle
{
    private boolean isAsyncView, hasResumed = false;
    private AsyncLayoutInflater layoutInflater = null;
    private BaseCycleViewModel _cycleViewModel = null;

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState)
    {
        // 初始化
        _cycleViewModel = Ext.create(this, BaseCycleViewModel.class);
        // 发送初始化数据的信息
        _cycleViewModel.cycleStatus.setValue(Status.INIT_OBJ);

        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        // 监听各个事件
        _cycleViewModel.cycleStatus.observe(this, new ObserverNoneNull<Status>()
        {
            @Override
            public void changed(@NonNull Status status)
            {
                switch (status)
                {
                    case INIT_OBJ:
                        initObj();
                        break;
                    case INIT_DATA:
                        initData(savedInstanceState);
                        break;
                    case INIT_VIEW:
                        initView();
                        break;
                    case INIT_EVENT:
                        initEvent();
                        break;
                    case LOAD_DATA:
                        loadData();
                        break;
                }
            }
        });
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState)
    {
        super.onPostCreate(savedInstanceState);
        _cycleViewModel.cycleStatus.setValue(Status.INIT_DATA);
        if (!isAsyncView)
        {
            _cycleViewModel.cycleStatus.setValue(Status.INIT_VIEW);
            _cycleViewModel.cycleStatus.setValue(Status.INIT_EVENT);
        }
    }

    @Override
    protected void onPostResume()
    {
        super.onPostResume();
        if (!hasResumed)
        {
            hasResumed = true;
            if (!isAsyncView)
            {
                _cycleViewModel.cycleStatus.setValue(Status.LOAD_DATA);
            }
        }
    }


    // 初始化与生命周期无关的对象
    @Override
    public void initObj()
    {
    }

    // 初始化传递的数据
    @Override
    public abstract void initData(@Nullable Bundle savedInstanceState);

    // 初始化界面
    @Override
    public abstract void initView();

    // 初始化各种事件
    @Override
    public abstract void initEvent();

    // 开始加载数据
    @Override
    public abstract void loadData();

    @Override
    protected void onDestroy()
    {
        _cycleViewModel.cycleStatus.removeObservers(this);
        super.onDestroy();
    }

    @Override
    public void setContentView(int layoutResID)
    {
        isAsyncView = true;
        if (layoutInflater == null)
        {
            layoutInflater = new AsyncLayoutInflater(this);
        }
        layoutInflater.inflate(layoutResID, null, new AsyncLayoutInflater.OnInflateFinishedListener()
        {
            @Override
            public void onInflateFinished(@NonNull View view, int resid, @Nullable ViewGroup parent)
            {
                if (!isFinishing() && !isDestroyed())
                {
                    setContentView(view);
                    _cycleViewModel.cycleStatus.setValue(Status.INIT_VIEW);
                    _cycleViewModel.cycleStatus.setValue(Status.INIT_EVENT);
                    _cycleViewModel.cycleStatus.setValue(Status.LOAD_DATA);
                }
            }
        });
        // super.setContentView(layoutResID);
    }
}
